﻿using System;
using System.ComponentModel;
using System.Data.EntityClient;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace EQEmu
{
    public partial class grid_entries
    {
        [XmlIgnore]
        public float HeadingDegrees
        {
            get
            {
                return this.heading / 512 * 360;
            }
            set
            {
                this.heading = value / 360 * 512;
            }
        }

        public void LookAt(double x, double y, double z)
        {
            double a = x - this.x;
            double b = y - this.y;
            double degrees = Math.Atan(b / a) * 180 / Math.PI;

            if (a == 0)
            {
                if (b > 0) degrees = 45.0;
                else if (b < 0) degrees = 270.0;
                else degrees = 0.0;
            }
            else if (b == 0)
            {
                if (a > 0) degrees = 0.0;
                else if (a < 0) degrees = 180.0;
                else degrees = 0.0;
            }
            else
            {
                if (a > 0)
                {
                    if (b > 0)
                    {
                        //quadrant 1                   
                        degrees = (90 - degrees) % 360;
                    }
                    else if (b < 0)
                    {
                        //quadrant 4
                        degrees = (90 - degrees) % 360;
                    }
                }
                else
                {
                    if (b > 0)
                    {
                        //quadrant 2
                        degrees = (270 - degrees) % 360;
                    }
                    else
                    {
                        //quadrant 3
                        //degrees = (180 + degrees) % 360;
                        degrees = (270 - degrees) % 360;
                    }
                }
            }
            degrees += 90;
            this.HeadingDegrees = (float)degrees;
        }

        public override string ToString()
        {
            return this.number.ToString() + ":" + this.name;
        }
    }
}
